package com.jdrx.sw.sewage.service;

import com.github.pagehelper.PageHelper;
import com.jdrx.ocp.beans.entity.UserPO;
import com.jdrx.ocp.dao.UserDAO;
import com.jdrx.sw.sewage.beans.dto.MedicineAnalysisAddDTO;
import com.jdrx.sw.sewage.beans.dto.MedicineAnalysisQueryDTO;
import com.jdrx.sw.sewage.beans.dto.StationIdDTO;
import com.jdrx.sw.sewage.beans.entity.MedicineAnalysisPO;
import com.jdrx.sw.sewage.beans.vo.DrugConsStatisticsVO;
import com.jdrx.sw.sewage.beans.vo.MedicineAnalysisVO;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.common.util.LocalDateTimeUtils;
import com.jdrx.sw.sewage.dao.MedicineAnalysisDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MedicineAnalysisService {
    @Autowired
    private MedicineAnalysisDAO medicineAnalysisDAO;
    @Autowired
    private UserDAO userDAO;

    public void add(MedicineAnalysisAddDTO dto, Long uid) {
        MedicineAnalysisPO po = new MedicineAnalysisPO();
        BeanUtils.copyProperties(dto, po);
        po.setCreateBy(uid);
        po.setNoteTaker(uid);
        po.setTotalPrice(dto.getAmount() * dto.getPrice());
        // TODO AI计算单位药耗比需取同类药剂上次加药时间至本次加药时间段内的点位药剂量、点位流量值
        medicineAnalysisDAO.insert(po);
    }

    public PageVO<MedicineAnalysisVO> pageList(MedicineAnalysisQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        MedicineAnalysisPO po = new MedicineAnalysisPO();
        BeanUtils.copyProperties(dto, po);
        List<MedicineAnalysisVO> medicineAnalysisVOS = medicineAnalysisDAO.listBy(po);
        List<UserPO> userPOS = userDAO.listBy(null);
        medicineAnalysisVOS.forEach(item -> {
            UserPO userPO = userPOS.stream().filter(a -> a.getId().equals(item.getNoteTaker())).findAny().orElse(null);
            if (userPO != null) {
                item.setNoteTakerName(userPO.getRealName());
            }
        });
        return PageVO.toPageVO(medicineAnalysisVOS);
    }

    public Map<String, List<DrugConsStatisticsVO>> drugConsStatistics(StationIdDTO dto) throws Exception {
        List<DrugConsStatisticsVO> list = medicineAnalysisDAO.drugConsStatistics(dto);
        Map<String, List<DrugConsStatisticsVO>> map = list.stream().collect(Collectors.groupingByConcurrent(DrugConsStatisticsVO::getCategoryName));
        //封装list
        for (String key : map.keySet()) {
            List<DrugConsStatisticsVO> drugConsStatisticsVOS = map.get(key);
            LocalDateTime intervalTime = drugConsStatisticsVOS.get(0).getIntervalTime();
            int L = LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDate2String(intervalTime) + " 00:00:00", LocalDateTimeUtils.localDateTime2String(intervalTime)).intValue();
            for (int a = L; a > 0; a--) {
                DrugConsStatisticsVO VO = new DrugConsStatisticsVO();
                Date date = LocalDateTimeUtils.stepHour(Date.from(intervalTime.atZone(ZoneId.systemDefault()).toInstant()), a);
                VO.setCategoryName(key);
                VO.setIntervalTime(date.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime());
                VO.setAmount(null);
                drugConsStatisticsVOS.add(0, VO);
            }
            drugConsStatisticsVOS = drugConsStatisticsVOS.stream().sorted(Comparator.comparing(DrugConsStatisticsVO::getIntervalTime)).collect(Collectors.toList());
            map.put(key, drugConsStatisticsVOS);
        }
        return map;
    }

/*    public static void main(String[] args) throws Exception {
        LocalDateTime intervalTime = LocalDateTime.now();
        int L = LocalDateTimeUtils.timeDiff(LocalDateTimeUtils.localDate2String(intervalTime) + " 00:00:00", LocalDateTimeUtils.localDateTime2String(intervalTime)).intValue();
        for (int a = L; a > 0; a--) {
            Date date = LocalDateTimeUtils.stepHour(Date.from(intervalTime.atZone(ZoneId.systemDefault()).toInstant()), a);
            System.out.println(date);
        }
    }*/
}